using System.Collections.Generic;

namespace StackCalc.Instructions
{
	public class Negation : IInstruction
	{
		public void Execute(List<IInstruction> stack, StackEngine stackEngine)
		{
			// pop argument
			INumericInstruction argument = stack.PeekInstructionFromTop(1) as INumericInstruction;

			if (argument != null)
			{
				// pop old number
				stack.PopInstructionFromTop(1);

				// push negated number
				stack.Add(new PushInteger(-argument.NumericValue));
			}
		}

		public string Validate(List<IInstruction> stack)
		{
			if (stack.Count == 0)
			{
				return "Not enough elements on stack!";
			}

			// get argument, should be numeric
			INumericInstruction argument = stack.PeekInstructionFromTop(1) as INumericInstruction;

			if (argument == null)
			{
				return "Argument not numeric!";
			}

			return string.Empty;
		}
	}
}
